<?php

namespace App\Admin\Extensions;

use App\Models\GoldRecord;
use Carbon\Carbon;
use App\Models\GoldRound;
use App\Models\Statistic;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Layout\Column;
use Encore\Admin\Widgets\InfoBox;
use App\Services\Statistics\SearchCount;

class DashboardBoxes
{
	public function countBoxes()
	{
		return function (Row $row) {

			$this->goldRoundsState($row, 2);
			$this->allBudgetUsersCount($row, 2);
			$this->allBudgetMoney($row, 2);
			$this->allEnteredUsersCount($row, 2);
			$this->allAliveStormProductsCount($row, 2);
			$this->allSoldStormProductsCount($row, 2);

			$this->allGoldRecordsMoney($row, 2);
			$this->todayGoldRecordsMoney($row, 2);
			$this->allResellingGoldBeans($row, 2);
			$this->todayResellingGoldBeans($row, 2);
			$this->allGoldBonuses($row, 2);
			$this->todayGoldBonuses($row, 2);

			$this->allGoldChargesMoney($row, 2);
			$this->todayGoldChargesMoney($row, 2);
			$this->allUsersCount($row, 2);
			$this->vipUsersCount($row, 2);
			$this->todayVipUsersCount($row, 2);

			$this->commentsCount($row, 2);

			$this->productSpu($row, 2);
			$this->productSku($row, 2);
			$this->ordersCount($row, 2);
			$this->productSpentAmount($row, 2);
			$this->productRefundsAmount($row, 2);
			$this->buyProductProfit($row, 2);

			$this->activeUsersCount($row, 2, 'month');
			$this->activeUsersCount($row, 2, 'week');
			$this->activeUsersCount($row, 2, 'day');

			$this->downloadsCount($row, 2);
			$this->downloadsCount($row, 2, 'android');
			$this->downloadsCount($row, 2, 'ios');
		};
	}


	public function countBoxesForService()
	{
		return function (Row $row) {

			$this->yesterdayUser($row, 2);
			$this->yesterdayValiUser($row, 2);
			$this->yesterdayChargeBeans($row, 2);
			$this->ChargeBeansAll($row, 2);
			$this->userAll($row, 2);
			$this->valiUserAll($row, 2);
			$this->todayUser($row, 2);
			$this->todayValiUser($row, 2);
			$this->todayChargeBeans($row, 2);
		};
	}


	protected function yesterdayUser(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchYesterdayUser();
			$box = new InfoBox('昨日新增会员人数', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}

		protected function todayUser(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchTodayUser();
			$box = new InfoBox('今日新增会员人数', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}



	protected function yesterdayValiUser(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchYesterdayValiUser();
			$box = new InfoBox('昨日有效会员人数', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}


	protected function todayValiUser(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchToValiUser();
			$box = new InfoBox('今日有效会员人数', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}

	protected function yesterdayChargeBeans(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchYesterdayChargeBeans();
			$box = new InfoBox('昨日充值金豆数', 'bitcoin', 'purple', '/admin/gold-beans', $money);
			$column->append($box);
		});
	}

	protected function todayChargeBeans(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchTodayChargeBeans();
			$box = new InfoBox('今日充值金豆数', 'bitcoin', 'purple', '/admin/gold-beans', $money);
			$column->append($box);
		});
	}


	protected function ChargeBeansAll(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchChargeBeansAll();
			$box = new InfoBox('累计充值金豆数', 'bitcoin', 'purple', '/admin/gold-beans', $money);
			$column->append($box);
		});
	}

	protected function userAll(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchUserAll();
			$box = new InfoBox('累计会员数', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}

	protected function valiUserAll(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchValiUserAll();
			$box = new InfoBox('累计有效会员数', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}

	protected function goldRoundsState(Row $row, $column)
	{
		$row->column($column, function (Column $column) {
			$content = "";
			$rounds = GoldRound::allAvailable();
			$time = Carbon::now();
			foreach ($rounds as $round) {

                if ($round['is_ending']) continue;

				$title = $round['title'];
				$openedAt = GoldRound::todayOpenedAt($round['id']);
				$startedAt = GoldRound::todayStartedAt($round['id']);
				$endedAt = GoldRound::todayEndedAt($round['id']);
				if ($time->lt($openedAt)) {
					$state = _label('未开场 待入场');
				} elseif ($time->gte($openedAt) and $time->lte($startedAt)) {
					$state = _label('已入场 待开始', 'success');
				} elseif ($time->gte($startedAt) and $time->lte($endedAt)) {
					$state = _label('已开始 抢购中', 'danger');
				} else {
					$state = _label('已结束 待开场');
				}
				$content .= "<small>{$title}：</small>{$state}<br>";
			}

			$box = new Box('金店场次状态', "{$content}");
			$box->style('danger')->solid();
			$column->append($box);
		});
	}

	protected function allAliveStormProductsCount(Row $row, $column)
	{
		$row->column($column, function (Column $column) {
			$content = "";
			$rounds = GoldRound::allAvailable();
			foreach ($rounds as $round) {

                if ($round['is_ending']) continue;

				$title = $round['title'];
				$count = GoldRound::sAllAliveGoldProductsCacheCount($round['id']);
				$content .= "<small>{$title}：</small>{$count}<br>";
			}

			$box = new Box('金店待抢记录', "{$content}");
			$box->style('success')->solid();
			$column->append($box);
		});
	}

	protected function allSoldStormProductsCount(Row $row, $column)
	{
		$row->column($column, function (Column $column) {
			$content = "";
			$rounds = GoldRound::allAvailable();
			foreach ($rounds as $round) {

                if ($round['is_ending']) continue;

				$title = $round['title'];
				$count = GoldRound::sAllSoldGoldProductsCacheCount($round['id']);
				$content .= "<small>{$title}：</small>{$count}<br>";
			}

			$box = new Box('金店已抢记录', "{$content}");
			$box->style('success')->solid();
			$column->append($box);
		});
	}

	protected function allBudgetUsersCount(Row $row, $column)
	{
		$row->column($column, function (Column $column) {
			$content = "";
			/*$rounds = GoldRound::allAvailable();
			foreach ($rounds as $round) {
				$title = $round['title'];
				$count = GoldRound::sAllGoldBudgetUsersCacheCount($round['id']);
				$content .= "<small>{$title}：</small>{$count}<br>";
			}*/

			$box = new Box('UNTITLED', "{$content}");
			$box->style('primary')->solid();
			$column->append($box);
		});
	}

	protected function allBudgetMoney(Row $row, $column)
	{
		$row->column($column, function (Column $column) {
			$content = "";
			/*$rounds = GoldRound::allAvailable();
			foreach ($rounds as $round) {
				$title = $round['title'];
				$money = GoldRound::sGoldBudgetCacheMoney($round['id']);
				$origin = $money['origin_money'];
				$used = $money['used_money'];
				$content .= "<small>{$title}：</small>{$origin}({$used})<br>";
			}*/

			$box = new Box('UNTITLED', "{$content}");
			$box->style('primary')->solid();
			$column->append($box);
		});
	}

	protected function allEnteredUsersCount(Row $row, $column)
	{
		$row->column($column, function (Column $column) {
			$content = "";
			/*$rounds = GoldRound::allAvailable();
			foreach ($rounds as $round) {
				$title = $round['title'];
				$enteredCount = GoldRound::sAllGoldEnterUsersCacheCount($round['id']);
				$finishedCount = GoldRound::sAllGoldEnterFinishedUsersCacheCount($round['id']);
				$content .= "<small>{$title}：</small>{$enteredCount}({$finishedCount})<br>";
			}*/

			$box = new Box('UNTITLED', "{$content}");
			$box->style('primary')->solid();
			$column->append($box);
		});
	}

	protected function allGoldRecordsMoney(Row $row, $column)
	{
		$row->column($column, function (Column $column) {
			$money = Statistic::fetch('total_gold_records_money_now');
			$box = new InfoBox('累计抢购金额', 'bitcoin', 'purple', '/admin/gold-records', $money);
			$column->append($box);
		});
	}

	protected function todayGoldRecordsMoney(Row $row, $column)
	{
		$row->column($column, function (Column $column) {
			$money = Statistic::fetch('total_gold_records_money_today');
			$box = new InfoBox('今日抢购金额', 'bitcoin', 'purple', '/admin/gold-records', $money);
			$column->append($box);
		});
	}

	protected function allResellingGoldBeans(Row $row, $column)
	{
		$row->column($column, function (Column $column) {
			$money = Statistic::fetch('total_gold_beans_fee_now');
			$box = new InfoBox('累计寄售手续费', 'bitcoin', 'purple', '/admin/gold-beans', $money);
			$column->append($box);
		});
	}

	protected function todayResellingGoldBeans(Row $row, $column)
	{
		$row->column($column, function (Column $column) {
			$bonus = Statistic::fetch('total_gold_beans_fee_today');
			$box = new InfoBox('今日寄售手续费', 'bitcoin', 'purple', '/admin/gold-beans', $bonus);
			$column->append($box);
		});
	}

	protected function allGoldBonuses(Row $row, $column)
	{
		$row->column($column, function (Column $column) {
			$bonus = Statistic::fetch('total_gold_bonus_money_now');
			$box = new InfoBox('累计金店奖励', 'bitcoin', 'purple', '/admin/gold-coins', $bonus);
			$column->append($box);
		});
	}

	protected function todayGoldBonuses(Row $row, $column)
	{
		$row->column($column, function (Column $column) {
			$bonus = Statistic::fetch('total_gold_bonus_money_today');
			$box = new InfoBox('今日金店奖励', 'bitcoin', 'purple', '/admin/fund-pools', $bonus);
			$column->append($box);
		});
	}

	protected function allGoldChargesMoney(Row $row, $column)
	{
		$row->column($column, function (Column $column) {
			$money = (int)Statistic::fetch('total_gold_charges_money_now');
			$box = new InfoBox('累计充值金额', 'users', 'light-blue', '/admin/gold-charges', $money);
			$column->append($box);
		});
	}

	protected function todayGoldChargesMoney(Row $row, $column)
	{
		$row->column($column, function (Column $column) {
			$money = (int)Statistic::fetch('total_gold_charges_money_today');
			$box = new InfoBox('今日充值金额', 'users', 'light-blue', '/admin/gold-charges', $money);
			$column->append($box);
		});
	}

	protected function allUsersCount(Row $row, $column)
	{
		$row->column($column, function (Column $column) {
			$count = (int)Statistic::fetch('total_users_count_now');
			$box = new InfoBox('会员数量累计', 'users', 'light-blue', '/admin/users', $count);
			$column->append($box);
		});
	}

	protected function vipUsersCount(Row $row, $column)
	{
		$row->column($column, function (Column $column) {
			$count = (int)Statistic::fetch('total_vip_users_count_now');
			$box = new InfoBox('VIP会员数量', 'users', 'light-blue', '/admin/users', $count);
			$column->append($box);
		});
	}

	protected function todayVipUsersCount(Row $row, $column)
	{
		$row->column($column, function (Column $column) {
			$count = (int)Statistic::fetch('total_vip_users_count_today');
			$box = new InfoBox('今日VIP会员数量', 'users', 'light-blue', '/admin/users', $count);
			$column->append($box);
		});
	}

	protected function commentsCount(Row $row, $column)
	{
		$row->column($column, function (Column $column) {
			$count = (int)Statistic::fetch('total_comments_now');
			$box = new InfoBox('评论数量累计', 'file-text', 'light-blue', '/admin/comments', $count);
			$column->append($box);
		});
	}

	protected function productSpu(Row $row, $column)
	{
		$row->column($column, function (Column $column) {
			$count = (int)Statistic::fetch('total_product_spus_now');
			$box = new InfoBox('商品 SPU', '', 'olive', '/admin/products', $count);
			$column->append($box);
		});
	}

	protected function productSku(Row $row, $column)
	{
		$row->column($column, function (Column $column) {
			$count = (int)Statistic::fetch('total_product_skus_now');
			$box = new InfoBox('商品 SKU', '', 'olive', '/admin/products', $count);
			$column->append($box);
		});
	}

	protected function ordersCount(Row $row, $column)
	{
		$row->column($column, function (Column $column) {
			$count = (int)Statistic::fetch('total_product_orders_count_now');
			$box = new InfoBox('订单数量累计', 'file-text', 'olive', '/admin/orders', $count);
			$column->append($box);
		});
	}

	protected function productSpentAmount(Row $row, $column)
	{
		$row->column($column, function (Column $column) {
			$sum = Statistic::fetch('total_product_orders_money_now');
			$box = new InfoBox('商品累计消费', 'bitcoin', 'olive', '/admin', $sum);
			$column->append($box);
		});
	}

	protected function productRefundsAmount(Row $row, $column)
	{
		$row->column($column, function (Column $column) {
			$sum = Statistic::fetch('total_product_orders_refund_now');
			$box = new InfoBox('商品累计退费', 'bitcoin', 'olive', '/admin', $sum);
			$column->append($box);
		});
	}

	protected function buyProductProfit(Row $row, $column)
	{
		$row->column($column, function (Column $column) {
			$profit = Statistic::fetch('total_product_orders_profit_now');
			$box = new InfoBox('商品销售利润', 'bitcoin', 'olive', '/admin/fund-pools', $profit);
			$column->append($box);
		});
	}

	protected function activeUsersCount(Row $row, $column, $type = 'month')
	{
		$row->column($column, function (Column $column) use ($type) {
			$text = '月活';
			if ($type == 'week') $text = '周活';
			if ($type == 'day') $text = '日活';
			//$count = counter('user')->activeUsersCount('month');
			$count = 0;
			$box = new Box($text, "{$count} ");
			$column->append($box);
		});
	}

	protected function downloadsCount(Row $row, $column, $type = null)
	{
		$row->column($column, function (Column $column) use ($type) {
			$text = '总下载量';
			if ($type == 'android') $text = 'ANDROID下载量';
			if ($type == 'ios') $text = 'IOS下载量';
			//$count = counter('user')->downloadsCount($type);
			$count = 0;
			$box = new Box($text, "{$count} ");
			$column->append($box);
		});
	}
}
